﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Odyssey.Linq;
using SunTzu.Utility;
using Rea.Web.Models;

namespace Odyssey.Web.Controllers
{
    public class HomeController : FrontBaseController
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            BindTopNavigation();

            //TODO:切换效果
            var imageNews = GetTable<tb_article>()
                .Where(c => c.PicUrl != null && c.PicUrl.Length > 0 && c.ClassId == 5)
                .OrderByDescending(c => c.Id).Take(4);
            ViewBag.ImageNews = imageNews.ToList();

            // 官方新闻
            var officialNews = GetTable<tb_article>()
                .Where(c => c.IsHot && c.ClassId == 5)
                .OrderByDescending(c => c.Id).Take(6);
            ViewBag.OfficialNews = officialNews.ToList();

            // 分会场
            var branchNews = GetTable<tb_article>()
                .Where(c => c.IsHot && c.ClassId == 11)
                .OrderByDescending(c => c.Id).Take(6);
            ViewBag.BranchNews = branchNews.ToList();

            // 展会通告
            var noticeNews = GetTable<tb_article>()
                .Where(c => c.IsHot && c.ClassId == 12)
                .OrderByDescending(c => c.Id).Take(4);
            ViewBag.NoticeNews = noticeNews.ToList();

            // 活动列表
            var activesNews = GetTable<tb_article>()
                .Where(c => c.IsHot && c.ClassId == 13)
                .OrderByDescending(c => c.Id).Take(6);
            ViewBag.ActivesNews = activesNews.ToList();

            var projects = GetTable<form_project>().Where(c => c.State == 1).OrderByDescending(c => c.CreateDate).Take(10).ToList();
            var referrals = GetTable<form_referral_apply>().Where(c => c.State == 1).OrderByDescending(c => c.CreateDate).Take(10).ToList();
            ViewBag.Projects = projects;
            ViewBag.Referral = referrals;

            if (Request["old"] != null)
            {
                return View("index_old");
            }
            return View();
        }

        public ActionResult CityBBS()
        {
            return View();
        }

        public ActionResult UniverComp()
        {
            return View();
        }

        public ActionResult BusinessPlatform()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Logout()
        {
            LoginHelper.Logout();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult DoLogin(string email, string password)
        {
            if (email.IsNullOrEmpty() || password.IsNullOrEmpty())
            {
                SetMessage("Email地址或密码不能够为空！");
            }
            else if (LoginHelper.Login(email, password))
            {
                var returnUrl = "/UserCenter";
                if (Request["ReturnUrl"].IsNotNullOrEmpty())
                {
                    returnUrl = Request["ReturnUrl"];
                }
                return Redirect(returnUrl);
            }
            TempData["Email"] = email;
            return View("Login");
        }
    }
}
